﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CloudDcPc
{
    /// <summary>
    /// SetDlg.xaml 的交互逻辑
    /// </summary>
    public partial class SetDlg : Window
    {
        public SetDlg()
        {
            InitializeComponent();

            if (App.user.Type < 3)
            {
                type3_print_set.Visibility = Visibility.Collapsed;
                power_set.Visibility = Visibility.Collapsed;
            }
            else if (App.user.Type != 4)
            {
                power_set.Visibility = Visibility.Collapsed;
            }
            
          

            ////读取配置
            //slider_tb_fresh_time.Value = Properties.Settings.Default.set_refresh_time;
            ckb_autologin.IsChecked = Properties.Settings.Default.set_auto_login;
            ckb_savepwd.IsChecked = Properties.Settings.Default.set_save_pwd;
            txt_print_title.Text = Properties.Settings.Default.save_print_title;
            txt_print_foot.Text = Properties.Settings.Default.save_print_foot;
            txt_jl_power_time.Text = Properties.Settings.Default.set_jl_power_time+"";
            txt_sy_power_time.Text = Properties.Settings.Default.set_sy_power_time + "";
            food_unit.Text = Properties.Settings.Default.save_food_unit;
            food_type.Text = Properties.Settings.Default.save_food_type;
            food_suffix.Text = Properties.Settings.Default.save_food_suffix;
            txt_print_vip_nums.Text = Properties.Settings.Default.save_print_vip_nums + "";
            txt_print_normal_nums.Text = Properties.Settings.Default.save_print_normal_nums + "";
            ////读取配置

            

        }

        /// <summary>
        /// 关闭按钮
        /// </summary>
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 保存设置，按钮Tag为1代表“确认”
        /// </summary>
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            if (txt_pre_pwd.Password != "")
            {
                if (txt_new_pwd1.Password == "")
                {
                    MessageBox.Show("请输入新密码！");
                    return;
                }
                if (txt_new_pwd1.Password != txt_new_pwd2.Password)
                {
                    MessageBox.Show("新密码二次输入不一致！");
                    return;
                }
                if (txt_new_pwd1.Password.Length>32)
                {
                    MessageBox.Show("新密码长度过长，最多32位！");
                    return;
                }

                if (!App.UserBean.UpdateUserPwd(App.user, txt_new_pwd1.Password))
                {
                    MessageBox.Show("密码修改失败！");
                }               
            }

            if (txt_print_title.Text == "")
            {
                MessageBox.Show("请输入结账单打印标题！");
                return;
            }
            if (txt_print_foot.Text == "")
            {
                MessageBox.Show("请输入结账单打印标题！");
                return;
            }
            int jl_time, sy_time,print_vip_nums,print_normal_nums;
            try
            {
                jl_time = Int32.Parse(txt_jl_power_time.Text);
                sy_time = Int32.Parse(txt_sy_power_time.Text);
                print_vip_nums = Int32.Parse(txt_print_vip_nums.Text);
                print_normal_nums = Int32.Parse(txt_print_normal_nums.Text);
            }
            catch
            {
                MessageBox.Show("天数和打印数只能是整数！");
                return;
            }
            if (jl_time < 0 || sy_time < 0 || print_vip_nums < 0 || print_normal_nums<0)
            {
                MessageBox.Show("天数和打印数只能是正整数！");
                return;
            }
           // Properties.Settings.Default.set_refresh_time = slider_tb_fresh_time.Value;
            Properties.Settings.Default.set_auto_login = (bool)ckb_autologin.IsChecked;
            Properties.Settings.Default.set_save_pwd = (bool)ckb_savepwd.IsChecked;
            Properties.Settings.Default.save_print_title = txt_print_title.Text;
            Properties.Settings.Default.save_print_foot = txt_print_foot.Text;
            Properties.Settings.Default.set_jl_power_time = Int32.Parse(txt_jl_power_time.Text);
            Properties.Settings.Default.set_sy_power_time = Int32.Parse(txt_sy_power_time.Text);
            Properties.Settings.Default.save_food_unit = food_unit.Text;
            Properties.Settings.Default.save_food_type = food_type.Text;
            Properties.Settings.Default.save_food_suffix = food_suffix.Text;
            Properties.Settings.Default.save_print_vip_nums = print_vip_nums;
            Properties.Settings.Default.save_print_normal_nums = print_normal_nums;
            Properties.Settings.Default.Save();
            MessageBox.Show("保存成功！");
            if (((Button)sender).Tag.ToString()=="1")
            {
                this.Close();
            }


        }
    }
}
